package com.yunjian.core.service.impl;

import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.yunjian.core.service.JPushService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class JPushServiceImpl implements JPushService {

    @Value("${jpush.appKey}")
    private String appKey = "";

    @Value("${jpush.masterSecret}")
    private String masterSecret = "";

    /*ios 推送环境*/
    @Value("${jpush.apnsproduction}")
    private boolean apnsproduction;

    @Override
    public void pushAline(List<String> aliasList, String title, String content, Map<String, String> data) {
        ClientConfig config = ClientConfig.getInstance();
        JPushClient client = new JPushClient(masterSecret, appKey, null, config);
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())//推送平台
                .setAudience(Audience.alias(aliasList))//推送目标
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(
                                AndroidNotification.newBuilder()
                                        .setTitle(title)
                                        .setAlert(content)
                                        .addExtras(data)
                                        .build())
                        .addPlatformNotification(
                                IosNotification.newBuilder()
                                        .incrBadge(1)
                                        .setAlert(content)
                                        .addExtras(data)
                                        .setSound("default")
                                        .setContentAvailable(true)
                                        .build()
                        )
                        .build())//通知消息（标题，内容）
                .setOptions(Options.newBuilder()
                        .setApnsProduction(apnsproduction)
                        .setTimeToLive(86400)
                        .build()//离线消息保存时间
                )
                .build();
        try {
            PushResult result = client.sendPush(payload);//推送
            System.out.println("推送结果：" + result.getResponseCode());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
